/**
 *
 */
package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;
import com.nest.WorkNest.view.ListViewForScrollView;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyRecommendAct extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recommend_listview)
    ListViewForScrollView list;
    private ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private Context context = MyRecommendAct.this;
    private HolderAdapter<HashMap<String, String>> mAdapter;
    private SwipeRefreshLayout mSwipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_myrecommend);
        ButterKnife.bind(this);
        initView();
        loadingMyRecommend();
    }

    private void initView() {
        initBar();
        createAdapter();
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("我的推款");
        tv_actionbar_tab_side_right.setVisibility(View.VISIBLE);
        tv_actionbar_tab_side_right.setText("我要推款");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        tabBack();
        tv_actionbar_tab_side_right.setOnClickListener(this);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.rl_modulename_refresh);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
    }

    public void createAdapter() {
        HolderAdapter.ViewCreator<HashMap<String, String>> creator = new HolderAdapter.ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {

                return LayoutInflater.from(context).inflate(R.layout.items_listview_recommend, null);
            }

            @Override
            public void updateView(View view, int position, HashMap<String, String> map) {
                TextView proName = (TextView) view.findViewById(R.id.recommend_proname);
                TextView orderCount = (TextView) view.findViewById(R.id.recommend_order_count);
                TextView finishCount = (TextView) view.findViewById(R.id.recommend_order_finish_count);
                TextView price = (TextView) view.findViewById(R.id.recommend_price);
//                  push = (TextView) view.findViewById(R.id.recommend_push);
                ImageView recommendImg = (ImageView) view.findViewById(R.id.recommend_img);
                proName.setText(map.get("productName"));
                orderCount.setText(map.get("bookedQuantity"));
//                  finishCount.setText(map.get("finishCount"));
                price.setText(map.get("quotation"));
//                   push.setText(map.get("push"));
                String imgUrl = data.get(position).get("mainImage");
                if (!TextUtils.isEmpty(imgUrl)) {
                    if (!imgUrl.substring(0, 1).equals("h") && !imgUrl.substring(0, 1).equals("H")) {
                        imgUrl = "http://" + imgUrl;
                    }
                }
                Glide.with(context)
                        .load(imgUrl)
                        .centerCrop()
                        .placeholder(R.drawable.empty_photo)
                        .crossFade()
                        .into(recommendImg);
            }
        };
        mAdapter = new HolderAdapter<>(this.getLayoutInflater(), creator);
        list.setAdapter(mAdapter);
        mAdapter.update(data);
    }

    private void loadingMyRecommend() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
//                Logger.json(result);
                JSONArray jsonArray;
                if (Tool.getString(result, "resultCode").equals("0")) {
                    jsonArray = Tool.getJsonArray(result, "datalist");
                    data = Tool.getListMapByJsonArray(jsonArray);
                    mAdapter.update(data);
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                } else {
                    showToast(Tool.getString(result, "MSG"));
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostProductSelectRecords(context, "5", "",ioAuthCallBack);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_actionbar_tab_side_right:
                Intent intent = new Intent(context, CategoryAct.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);
                loadingMyRecommend();
            }
        }, 2000);
    }
}
